package cn.seecoder.courselearning.serviceimpl.post;

import cn.seecoder.courselearning.mapperservice.post.PostMapper;
import cn.seecoder.courselearning.po.course.Course;
import cn.seecoder.courselearning.po.order.CourseOrder;
import cn.seecoder.courselearning.po.post.Post;
import cn.seecoder.courselearning.service.post.PostService;
import cn.seecoder.courselearning.util.Constant;
import cn.seecoder.courselearning.util.PageInfoUtil;
import cn.seecoder.courselearning.vo.ResultVO;
import cn.seecoder.courselearning.vo.course.CourseVO;
import cn.seecoder.courselearning.vo.post.PostVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class PostServiceImpl implements PostService {
    @Resource
    private PostMapper postMapper;

    @Override
    public ResultVO<PostVO> createPost(PostVO postVO, Integer courseId) {
//        courseVO.setCreateTime(new Date());
//        for(Course course: courseMapper.selectByTeacherId(courseVO.getTeacherId())){
//            if (course.getName().equals(courseVO.getName()))
//                return new ResultVO<>(Constant.REQUEST_FAIL, "已存在同名课程！");
//        }
        Post post = new Post(postVO);
        post.setCourseId(courseId);
        if(postMapper.insert(post) > 0){
            return new ResultVO<PostVO>(Constant.REQUEST_SUCCESS, "发帖成功。", new PostVO(post));
        }
        return new ResultVO<>(Constant.REQUEST_FAIL, "服务器错误");
    }

    @Override
    public PageInfo<PostVO> getPostsByType(Integer currPage, Integer pageSize, Integer courseId, String type) {
        if(currPage==null || currPage<1) currPage=1;
        PageHelper.startPage(currPage, pageSize);
        PageInfo<Post> po = new PageInfo<>(postMapper.selectByCourse(courseId));
        List<Post> polist = postMapper.selectByCourse(courseId);
//        po=sortByType(po,type);
        System.out.println(polist);
        System.out.println(po);
        return getPostVOPageInfo(po);
    }

    private PageInfo<PostVO> getPostVOPageInfo(PageInfo<Post> po) {
        PageInfo<PostVO> result = PageInfoUtil.convert(po, PostVO.class);
        return result;
    }

    private PageInfo<Post> sortByType(PageInfo<Post> po,String type){
        //Todo
        return null;
    }
}
